package com.synchrona.pluginManager;

import com.synchrona.account.Account;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.Status;


public class TwitterPlugin {

	public TwitterPlugin() {
		System.out.println("Twitter Plugin...");
	}

	// updates the user status
	public void updateStatus(String status, Account account) {
		String twitterId = account.getServiceUserName();
		String twitterPassword = account.getServicePassword();
		Twitter twitter = new TwitterFactory().getInstance(twitterId,twitterPassword);
		Status statu;
		try {
			statu = twitter.updateStatus(status);
			System.out.println("Twitter Status updated " + statu.getText());
		} catch (TwitterException e) {
			e.printStackTrace();
		}
	}

	public void getTweets(Account account) {
		try {
			System.out.println(account.getServiceUserName() + " "
					+ account.getServicePassword());
			String twitterId = account.getServiceUserName();
			String twitterPassword = account.getServicePassword();
			Twitter twitter = new TwitterFactory().getInstance(twitterId,
					twitterPassword);
			Query query = new Query("@" + account.getServiceUserName());
			QueryResult result = twitter.search(query);

			System.out.println("hits:" + result.getTweets().size());
			int count = 0;
			for (Tweet tweet : result.getTweets()) {
				System.out.println(tweet.getFromUser() + ":" + tweet.getText());
				if (count == 5) {
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * // returns all the updates in a user profile public void GetUserTime() {
	 * String twitterId = null; String twitterPassword = null;
	 * 
	 * Twitter twitter = new TwitterFactory().getInstance(twitterId,
	 * twitterPassword); List<Status> statuses;
	 * 
	 * try { statuses = twitter.getFriendsTimeline();
	 * System.out.println("Showing Frinends Timeline:"); for (Status status :
	 * statuses) { System.out.println(status.getUser().getName() + ":" +
	 * status.getCreatedAt() + ":" + status.getText()); } } catch (Exception e)
	 * { e.printStackTrace(); }
	 * 
	 * }
	 */
}
